﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using Table.Blo;
using UI;
using UnityEngine;
using UnityEngine.UI;
using Yunchang.NetworkLayer;

public class ChessPackagesPage : OPPage
{
    public TextureKit backGroud;
    public Transform itemRoot;
    public ChessPackageItem prefab;

    private Dictionary<int,HeroExtPackage> cacheData = new Dictionary<int,HeroExtPackage>();
    private Dictionary<int, ChessPackageItem> packageMap = new Dictionary<int, ChessPackageItem>();

    public GameObject btnLeft, btnRight;
    public ScrollRect btnScrollRect;
    
    private void Update()
    {
        if (cacheData.Count>4 )
        {
            UICommonMethod.UpdateScrollerArrows(btnScrollRect, btnLeft, btnRight, false);
        }
    }

    protected override void DoOpen()
    {
        base.DoOpen();
        Register();
        InitPackageItems();
        RequrestNet();
    }

    private void RequrestNet()
    {
        NetworkDriver.Instance.SendSocketBlock(OpDefine.CSAutoChessGetExtraPackInfo, new CSAutoChessGetExtraPackInfo(), OpDefine.SCAutoChessGetExtraPackInfo);
    }

    protected override void DoClose(bool immediately)
    {
        base.DoClose(immediately);
        UnRegister();
    }

    private void Register()
    {
        NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessGetExtraPackInfo, OnSCAutoChessGetExtraPackInfo);
        NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessBuyPacket, OnSCAutoChessBuyPacket);
    }

    private void UnRegister()
    {
        NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessGetExtraPackInfo, OnSCAutoChessGetExtraPackInfo);
        NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessBuyPacket, OnSCAutoChessBuyPacket);
    }

    private void InitPackageItems()
    {
        itemRoot.DestroyChildren();
        backGroud.TextureName = Table.DataModel.TableConfig.ChessPackageBackground;
        cacheData = AutoChessBlo.GetAllHeroPackage();
        HeroExtPackage tempData = null;
        foreach (var item in cacheData)
        {
            tempData = item.Value;
            var script = Instantiate<ChessPackageItem>(prefab, itemRoot);
            script.InitItem(tempData, OnChangeItem);
            packageMap[tempData.packageId] = script;
        }
    }
    private void OnChangeItem(string packagebg)
    {
        backGroud.TextureName = packagebg;
    }
    private void OnSCAutoChessGetExtraPackInfo(object msg)
    {
        var data = msg as SCAutoChessGetExtraPackInfo;
        RefreshPackageServerData(data);
        RefreshPackageItemData();
    }

    private void RefreshPackageServerData(SCAutoChessGetExtraPackInfo serverData)
    {
        AutochessExtraPackInfo tempPackage = null;
        HeroExtPackage heroTemp = null; ;
        for (int i = 0; i < serverData.packetInfos.Count; i++)
        {
            tempPackage = serverData.packetInfos[i];
            if (cacheData.TryGetValue(tempPackage.packetId, out heroTemp))
            {
                heroTemp.RefreshServerData(tempPackage, serverData.heroIds);
            }
            else
            {
                ExLog.LogError("拓展包服务器客户端数据不一致!");
            }
        }
    }

    private void RefreshPackageItemData()
    {
        foreach (var item in cacheData)
        {
            packageMap[item.Key].RefreshItem(item.Value);
        }
    }

    private void OnSCAutoChessBuyPacket(object msg)
    {
        RequrestNet();
    }
}
